home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / compiler / ir1util.lisp < prev    next >
Encoding:
Text File  |  1992-12-09  |  70.0 KB  |  2,079 lines

  1. ;;; -*- Package: C; Log: C.Log -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: ir1util.lisp,v 1.57 92/08/03 12:32:11 ram Exp $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;;    This file contains random utilities used for manipulating the IR1
  15. ;;; representation.
  16. ;;;
  17. ;;; Written by Rob MacLachlan
  18. ;;;
  19. (in-package "C")
  20. (export '(*compiler-notification-function*))
  21. (in-package "EXTENSIONS")
  22. (export '(*error-print-level* *error-print-length* *error-print-lines*
  23.       def-source-context *undefined-warning-limit*
  24.       *enclosing-source-cutoff*))
  25. (in-package "C")
  26.  
  27.  
  28. ;;;; Cleanup hackery:
  29.  
  30.  
  31. ;;; Node-Enclosing-Cleanup  --  Interface
  32. ;;;
  33. ;;;    Return the innermost cleanup enclosing Node, or NIL if there is none in
  34. ;;; its function.  If Node has no cleanup, but is in a let, then we must still
  35. ;;; check the environment that the call is in.
  36. ;;;
  37. (defun node-enclosing-cleanup (node)
  38.   (declare (type node node))
  39.   (do ((lexenv (node-lexenv node)
  40.            (lambda-call-lexenv (lexenv-lambda lexenv))))
  41.       ((null lexenv) nil)
  42.     (let ((cup (lexenv-cleanup lexenv)))
  43.       (when cup (return cup)))))
  44.  
  45.  
  46. ;;; Insert-Cleanup-Code  --  Interface
  47. ;;;
  48. ;;;    Convert the Form in a block inserted between Block1 and Block2 as an
  49. ;;; implicit MV-Prog1.  The inserted block is returned.  Node is used for IR1
  50. ;;; context when converting the form.  Note that the block is not assigned a
  51. ;;; number, and is linked into the DFO at the beginning.  We indicate that we
  52. ;;; have trashed the DFO by setting Component-Reanalyze.  If Cleanup is
  53. ;;; supplied, then convert with that cleanup.
  54. ;;;
  55. (defun insert-cleanup-code (block1 block2 node form &optional cleanup)
  56.   (declare (type cblock block1 block2) (type node node)
  57.        (type (or cleanup null) cleanup))
  58.   (setf (component-reanalyze (block-component block1)) t)
  59.   (with-ir1-environment node
  60.     (let* ((start (make-continuation))
  61.        (block (continuation-starts-block start))
  62.        (cont (make-continuation))
  63.        (*lexical-environment*
  64.        (if cleanup
  65.            (make-lexenv :cleanup cleanup)
  66.            *lexical-environment*)))
  67.       (change-block-successor block1 block2 block)
  68.       (link-blocks block block2)
  69.       (ir1-convert start cont form)
  70.       (setf (block-last block) (continuation-use cont))
  71.       block)))
  72.   
  73.  
  74. ;;;; Continuation use hacking:
  75.  
  76. ;;; Find-Uses  --  Interface
  77. ;;;
  78. ;;;    Return a list of all the nodes which use Cont.
  79. ;;;
  80. (proclaim '(function find-uses (continuation) list))
  81. (defun find-uses (cont)
  82.   (ecase (continuation-kind cont)
  83.     ((:block-start :deleted-block-start)
  84.      (block-start-uses (continuation-block cont)))
  85.     (:inside-block (list (continuation-use cont)))
  86.     (:unused nil)))
  87.  
  88.       
  89. ;;; Delete-Continuation-Use  --  Interface
  90. ;;;
  91. ;;;    Update continuation use information so that Node is no longer a use of
  92. ;;; its Cont.  If the old continuation doesn't start its block, then we don't
  93. ;;; update the Block-Start-Uses, since it will be deleted when we are done.
  94. ;;;
  95. ;;; Note: if you call this function, you may have to do a
  96. ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something has
  97. ;;; changed.
  98. ;;;
  99. (proclaim '(function delete-continuation-use (node) void))
  100. (defun delete-continuation-use (node)
  101.   (let* ((cont (node-cont node))
  102.      (block (continuation-block cont)))
  103.     (ecase (continuation-kind cont)
  104.       (:deleted)
  105.       ((:block-start :deleted-block-start)
  106.        (let ((uses (delete node (block-start-uses block))))
  107.      (setf (block-start-uses block) uses)
  108.      (setf (continuation-use cont)
  109.            (if (cdr uses) nil (car uses)))))
  110.       (:inside-block
  111.        (setf (continuation-kind cont) :unused)
  112.        (setf (continuation-block cont) nil)
  113.        (setf (continuation-use cont) nil)
  114.        (setf (continuation-next cont) nil)))
  115.     (setf (node-cont node) nil)))
  116.  
  117.  
  118. ;;; Add-Continuation-Use  --  Interface
  119. ;;;
  120. ;;;    Update continuation use information so that Node uses Cont.  If Cont is
  121. ;;; :Unused, then we set its block to Node's Node-Block (which must be set.)
  122. ;;;
  123. ;;; Note: if you call this function, you may have to do a
  124. ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something has
  125. ;;; changed.
  126. ;;;
  127. (proclaim '(function add-continuation-use (node continuation) void))
  128. (defun add-continuation-use (node cont)
  129.   (assert (not (node-cont node)))
  130.   (let ((block (continuation-block cont)))
  131.     (ecase (continuation-kind cont)
  132.       (:deleted)
  133.       (:unused
  134.        (assert (not block))
  135.        (let ((block (node-block node)))
  136.      (assert block)
  137.      (setf (continuation-block cont) block))
  138.        (setf (continuation-kind cont) :inside-block)
  139.        (setf (continuation-use cont) node))
  140.       ((:block-start :deleted-block-start)
  141.        (let ((uses (cons node (block-start-uses block))))
  142.      (setf (block-start-uses block) uses)
  143.      (setf (continuation-use cont)
  144.            (if (cdr uses) nil (car uses)))))))
  145.   (setf (node-cont node) cont))
  146.  
  147.  
  148. ;;; Immediately-Used-P  --  Interface
  149. ;;;
  150. ;;;    Return true if Cont is the Node-Cont for Node and Cont is transferred to
  151. ;;; immediately after the evaluation of Node.
  152. ;;;
  153. (defun immediately-used-p (cont node)
  154.   (declare (type continuation cont) (type node node))
  155.   (and (eq (node-cont node) cont)
  156.        (not (eq (continuation-kind cont) :deleted))
  157.        (let ((cblock (continuation-block cont))
  158.          (nblock (node-block node)))
  159.      (or (eq cblock nblock)
  160.          (let ((succ (block-succ nblock)))
  161.            (and (= (length succ) 1)
  162.             (eq (first succ) cblock)))))))
  163.  
  164.  
  165. ;;;; Continuation substitution:
  166.  
  167. ;;; Substitute-Continuation  --  Interface
  168. ;;;
  169. ;;;    In Old's Dest, replace Old with New.  New's Dest must initially be NIL.
  170. ;;; When we are done, we call Flush-Dest on Old to clear its Dest and to note
  171. ;;; potential optimization opportunities.
  172. ;;;
  173. (defun substitute-continuation (new old)
  174.   (declare (type continuation old new))
  175.   (assert (not (continuation-dest new)))
  176.   (let ((dest (continuation-dest old)))
  177.     (etypecase dest
  178.       ((or ref bind))
  179.       (cif (setf (if-test dest) new))
  180.       (cset (setf (set-value dest) new))
  181.       (creturn (setf (return-result dest) new))
  182.       (exit (setf (exit-value dest) new))
  183.       (basic-combination
  184.        (if (eq old (basic-combination-fun dest))
  185.        (setf (basic-combination-fun dest) new)
  186.        (setf (basic-combination-args dest)
  187.          (nsubst new old (basic-combination-args dest))))))
  188.  
  189.     (flush-dest old)
  190.     (setf (continuation-dest new) dest))
  191.   (undefined-value))
  192.  
  193.  
  194. ;;; Ensure-Block-Start  --  Interface
  195. ;;;
  196. ;;;    Ensure that Cont is the start of a block (or deleted) so that the use
  197. ;;; set can be freely manipulated.
  198. ;;; -- If the continuation is :Unused or is :Inside-Block and the Cont of Last
  199. ;;;    in its block, then we make it the start of a new deleted block.
  200. ;;; -- If the continuation is :Inside-Block inside a block, then we split the
  201. ;;;    block using Node-Ends-Block, which makes the continuation be a
  202. ;;;    :Block-Start.
  203. ;;;
  204. (defun ensure-block-start (cont)
  205.   (declare (type continuation cont))
  206.   (let ((kind (continuation-kind cont)))
  207.     (ecase kind
  208.       ((:deleted :block-start :deleted-block-start))
  209.       ((:unused :inside-block)
  210.        (let ((block (continuation-block cont)))
  211.      (cond ((or (eq kind :unused)
  212.             (eq (node-cont (block-last block)) cont))
  213.         (setf (continuation-block cont)
  214.               (make-block-key :start cont  :component nil
  215.                       :start-uses (find-uses cont)))
  216.         (setf (continuation-kind cont) :deleted-block-start))
  217.            (t
  218.         (node-ends-block (continuation-use cont))))))))
  219.   (undefined-value))
  220.  
  221.  
  222. ;;; Substitute-Continuation-Uses  --  Interface
  223. ;;;
  224. ;;;    Replace all uses of Old with uses of New, where New has an arbitary
  225. ;;; number of uses.  If New will end up with more than one use, then we must
  226. ;;; arrange for it to start a block if it doesn't already.
  227. ;;;
  228. (defun substitute-continuation-uses (new old)
  229.   (declare (type continuation old new))
  230.   (unless (and (eq (continuation-kind new) :unused)
  231.            (eq (continuation-kind old) :inside-block))
  232.     (ensure-block-start new))
  233.   
  234.   (do-uses (node old)
  235.     (delete-continuation-use node)
  236.     (add-continuation-use node new))
  237.  
  238.   (reoptimize-continuation new)
  239.   (undefined-value))
  240.  
  241.  
  242. ;;;; Misc shortand functions:
  243.  
  244. ;;; NODE-HOME-LAMBDA  --  Interface
  245. ;;;
  246. ;;;    Return the home (i.e. enclosing non-let) lambda for Node.  Since the
  247. ;;; LEXENV-LAMBDA may be deleted, we must chain up the LAMBDA-CALL-LEXENV
  248. ;;; thread until we find a lambda that isn't deleted, and then return its home.
  249. ;;;
  250. (declaim (maybe-inline node-home-lambda))
  251. (defun node-home-lambda (node)
  252.   (declare (type node node))
  253.   (do ((fun (lexenv-lambda (node-lexenv node))
  254.         (lexenv-lambda (lambda-call-lexenv fun))))
  255.       ((not (eq (functional-kind fun) :deleted))
  256.        (lambda-home fun))
  257.     (when (eq (lambda-home fun) fun)
  258.       (return fun))))
  259.  
  260. ;;; NODE-xxx  --  Interface
  261. ;;;
  262. (declaim (inline node-block node-tlf-number))
  263. (declaim (maybe-inline node-environment))
  264. (defun node-block (node)
  265.   (declare (type node node))
  266.   (the cblock (continuation-block (node-prev node))))
  267. ;;;
  268. (defun node-environment (node)
  269.   (declare (type node node) (inline node-home-lambda))
  270.   (the environment (lambda-environment (node-home-lambda node))))
  271.  
  272.  
  273. ;;; BLOCK-xxx-CLEANUP  --  Interface
  274. ;;;
  275. ;;;    Return the enclosing cleanup for environment of the first or last node
  276. ;;; in Block.
  277. ;;;
  278. (defun block-start-cleanup (block)
  279.   (declare (type cblock block))
  280.   (node-enclosing-cleanup (continuation-next (block-start block))))
  281. ;;;
  282. (defun block-end-cleanup (block)
  283.   (declare (type cblock block))
  284.   (node-enclosing-cleanup (block-last block)))
  285.  
  286.  
  287. ;;; BLOCK-HOME-LAMBDA  --  Interface
  288. ;;;
  289. ;;;    Return the non-let lambda that holds Block's code.
  290. ;;;
  291. (defun block-home-lambda (block)
  292.   (declare (type cblock block) (inline node-home-lambda))
  293.   (node-home-lambda (block-last block)))
  294.  
  295.  
  296. ;;; BLOCK-ENVIRONMENT  --  Interface
  297. ;;;
  298. ;;;    Return the IR1 environment for Block.
  299. ;;;
  300. (defun block-environment (block)
  301.   (declare (type cblock block) (inline node-home-lambda))
  302.   (lambda-environment (node-home-lambda (block-last block))))
  303.  
  304.  
  305. ;;; SOURCE-PATH-TLF-NUMBER  --  Interface
  306. ;;;
  307. ;;;    Return the Top Level Form number of path, i.e. the ordinal number of
  308. ;;; its orignal source's top-level form in its compilation unit.
  309. ;;;
  310. (defun source-path-tlf-number (path)
  311.   (declare (list path))
  312.   (car (last path)))
  313.  
  314.  
  315. ;;; SOURCE-PATH-ORIGINAL-SOURCE  --  Interface
  316. ;;;
  317. ;;;    Return the (reversed) list for the path in the orignal source (with the
  318. ;;; TLF number last.)
  319. ;;; 
  320. (defun source-path-original-source (path)
  321.   (declare (list path))
  322.   (cddr (member 'original-source-start path)))
  323.  
  324.  
  325. ;;; SOURCE-PATH-FORM-NUMBER  --  Interface
  326. ;;;
  327. ;;;    Return the Form Number of Path's orignal source inside the Top Level
  328. ;;; Form that contains it.  This is determined by the order that we walk the
  329. ;;; subforms of the top level source form.
  330. ;;;
  331. (defun source-path-form-number (path)
  332.   (declare (list path))
  333.   (cadr (member 'original-source-start path)))
  334.  
  335.  
  336. ;;; SOURCE-PATH-FORMS  --  Interface
  337. ;;;
  338. ;;;    Return a list of all the enclosing forms not in the original source that
  339. ;;; converted to get to this form, with the immediate source for node at the
  340. ;;; start of the list.
  341. ;;;
  342. (defun source-path-forms (path)
  343.   (subseq path 0 (position 'original-source-start path)))
  344.  
  345.  
  346. ;;; NODE-SOURCE-FORM  --  Interface
  347. ;;;
  348. ;;;    Return the innermost source form for Node.
  349. ;;;
  350. (defun node-source-form (node)
  351.   (declare (type node node))
  352.   (let* ((path (node-source-path node))
  353.      (forms (source-path-forms path)))
  354.     (if forms
  355.     (first forms)
  356.     (values (find-original-source path)))))
  357.  
  358.  
  359. ;;; CONTINUATION-SOURCE-FORM  --  Interface
  360. ;;;
  361. ;;;    Return NODE-SOURCE-FORM, T if continuation has a single use, otherwise
  362. ;;; NIL, NIL.
  363. ;;;
  364. (defun continuation-source (cont)
  365.   (let ((use (continuation-use cont)))
  366.     (if use
  367.     (values (node-source-form use) t)
  368.     (values nil nil))))
  369.  
  370.  
  371. ;;; MAKE-LEXENV  --  Interface
  372. ;;;
  373. ;;;    Return a new LEXENV just like Default except for the specified slot
  374. ;;; values.  Values for the alist slots are NCONC'ed to the beginning of the
  375. ;;; current value, rather than replacing it entirely.
  376. ;;; 
  377. (defun make-lexenv (&key (default *lexical-environment*)
  378.              functions variables blocks tags type-restrictions
  379.              inlines options
  380.              (lambda (lexenv-lambda default))
  381.              (cleanup (lexenv-cleanup default))
  382.              (cookie (lexenv-cookie default))
  383.              (interface-cookie (lexenv-interface-cookie default)))
  384.   (macrolet ((frob (var slot)
  385.            `(let ((old (,slot default)))
  386.           (if ,var
  387.               (nconc ,var old)
  388.               old))))
  389.     (internal-make-lexenv
  390.      (frob functions lexenv-functions)
  391.      (frob variables lexenv-variables)
  392.      (frob blocks lexenv-blocks)
  393.      (frob tags lexenv-tags)
  394.      (frob type-restrictions lexenv-type-restrictions)
  395.      (frob inlines lexenv-inlines)
  396.      lambda cleanup cookie interface-cookie
  397.      (frob options lexenv-options))))
  398.  
  399.  
  400. ;;; MAKE-INTERFACE-COOKIE  --  Interface
  401. ;;;
  402. ;;;    Return a cookie that defaults any unsupplied optimize qualities in the
  403. ;;; Interface-Cookie with the corresponding ones from the Cookie.
  404. ;;;
  405. (defun make-interface-cookie (lexenv)
  406.   (declare (type lexenv lexenv))
  407.   (let ((icookie (lexenv-interface-cookie lexenv))
  408.     (cookie (lexenv-cookie lexenv)))
  409.     (make-cookie
  410.      :speed (or (cookie-speed icookie) (cookie-speed cookie))
  411.      :space (or (cookie-space icookie) (cookie-space cookie))
  412.      :safety (or (cookie-safety icookie) (cookie-safety cookie))
  413.      :cspeed (or (cookie-cspeed icookie) (cookie-cspeed cookie))
  414.      :brevity (or (cookie-brevity icookie) (cookie-brevity cookie))
  415.      :debug (or (cookie-debug icookie) (cookie-debug cookie)))))
  416.                
  417.  
  418. ;;;; Flow/DFO/Component hackery:
  419.  
  420. ;;; Link-Blocks  --  Interface
  421. ;;;
  422. ;;;    Join Block1 and Block2.
  423. ;;;
  424. (defun link-blocks (block1 block2)
  425.   (declare (type cblock block1 block2))
  426.   (assert (not (member block2 (block-succ block1))))
  427.   (push block2 (block-succ block1))
  428.   (push block1 (block-pred block2))
  429.   (undefined-value))
  430.  
  431. ;;; UNLINK-BLOCKS  --  Interface
  432. ;;;
  433. ;;;    Like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2.  If this leaves a
  434. ;;; successor with a single predecessor that ends in an IF, then set
  435. ;;; BLOCK-TEST-MODIFIED so that any test constraint will now be able to be
  436. ;;; propagated to the successor.
  437. ;;;
  438. (defun unlink-blocks (block1 block2)
  439.   (declare (type cblock block1 block2))
  440.   (assert (member block2 (block-succ block1)))
  441.   (setf (block-succ block1)
  442.     (delete block2 (block-succ block1)))
  443.  
  444.   (let ((new-pred (delete block1 (block-pred block2))))
  445.     (setf (block-pred block2) new-pred)
  446.     (when (and new-pred (null (rest new-pred)))
  447.       (let ((pred-block (first new-pred)))
  448.     (when (if-p (block-last pred-block))
  449.       (setf (block-test-modified pred-block) t)))))
  450.   (undefined-value))
  451.  
  452.  
  453. ;;; Change-Block-Successor  --  Internal
  454. ;;;
  455. ;;;    Swing the succ/pred link between Block and Old to be between Block and
  456. ;;; New.  If Block ends in an IF, then we have to fix up the
  457. ;;; consequent/alternative blocks to point to New.  We also set
  458. ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to the new
  459. ;;; successor.
  460. ;;;
  461. (defun change-block-successor (block old new)
  462.   (declare (type cblock new old block))
  463.   (unlink-blocks block old)
  464.   (setf (component-reanalyze (block-component block)) t)
  465.   (unless (member new (block-succ block))
  466.     (link-blocks block new))
  467.   
  468.   (let ((last (block-last block)))
  469.     (when (if-p last)
  470.       (setf (block-test-modified block) t)
  471.       (macrolet ((frob (slot)
  472.            `(when (eq (,slot last) old)
  473.               (setf (,slot last) new))))
  474.     (frob if-consequent)
  475.     (frob if-alternative))))
  476.   
  477.   (undefined-value))
  478.  
  479.  
  480. ;;; Remove-From-DFO  --  Interface
  481. ;;;
  482. ;;;    Unlink a block from the next/prev chain.  We also null out the
  483. ;;; Component.
  484. ;;;
  485. (proclaim '(function remove-from-dfo (cblock) void))
  486. (defun remove-from-dfo (block)
  487.   (let ((next (block-next block))
  488.     (prev (block-prev block)))
  489.     (setf (block-component block) nil)
  490.     (setf (block-next prev) next)
  491.     (setf (block-prev next) prev)))
  492.  
  493. ;;; Add-To-DFO  --  Interface
  494. ;;;
  495. ;;;    Add Block to the next/prev chain following After.  We also set the
  496. ;;; Component to be the same as for After.
  497. ;;;
  498. (defun add-to-dfo (block after)
  499.   (declare (type cblock block after))
  500.   (let ((next (block-next after))
  501.     (comp (block-component after)))
  502.     (assert (not (eq (component-kind comp) :deleted)))
  503.     (setf (block-component block) comp)
  504.     (setf (block-next after) block)
  505.     (setf (block-prev block) after)
  506.     (setf (block-next block) next)
  507.     (setf (block-prev next) block))
  508.   (undefined-value))
  509.  
  510.  
  511. ;;; Clear-Flags  --  Interface
  512. ;;;
  513. ;;;    Set the Flag for all the blocks in Component to NIL, except for the head
  514. ;;; and tail which are set to T.
  515. ;;;
  516. (proclaim '(function clear-flags (component) void))
  517. (defun clear-flags (component)
  518.   (let ((head (component-head component))
  519.     (tail (component-tail component)))
  520.     (setf (block-flag head) t)
  521.     (setf (block-flag tail) t)
  522.     (do-blocks (block component)
  523.       (setf (block-flag block) nil))))
  524.  
  525.  
  526. ;;; Make-Empty-Component  --  Interface
  527. ;;;
  528. ;;;    Make a component with no blocks in it.  The Block-Flag is initially true
  529. ;;; in the head and tail blocks.
  530. ;;;
  531. (proclaim '(function make-empty-component () component))
  532. (defun make-empty-component ()
  533.   (let* ((head (make-block-key :start nil :component nil))
  534.      (tail (make-block-key :start nil :component nil))
  535.      (res (make-component :head head  :tail tail)))
  536.     (setf (block-flag head) t)
  537.     (setf (block-flag tail) t)
  538.     (setf (block-component head) res)
  539.     (setf (block-component tail) res)
  540.     (setf (block-next head) tail)
  541.     (setf (block-prev tail) head)
  542.     res))
  543.  
  544.  
  545. ;;; Node-Ends-Block  --  Interface
  546. ;;;
  547. ;;;    Makes Node the Last node in its block, splitting the block if necessary.
  548. ;;; The new block is added to the DFO immediately following Node's block.
  549. ;;;
  550. (defun node-ends-block (node)
  551.   (declare (type node node))
  552.   (let* ((block (node-block node))
  553.      (start (node-cont node))
  554.      (last (block-last block))
  555.      (last-cont (node-cont last)))
  556.     (unless (eq last node)
  557.       (assert (and (eq (continuation-kind start) :inside-block)
  558.            (not (block-delete-p block))))
  559.       (let* ((succ (block-succ block))
  560.          (new-block
  561.           (make-block-key :start start
  562.                   :component (block-component block)
  563.                   :start-uses (list (continuation-use start))
  564.                   :succ succ :last last)))
  565.     (setf (continuation-kind start) :block-start)
  566.     (dolist (b succ)
  567.       (setf (block-pred b)
  568.         (cons new-block (remove block (block-pred b)))))
  569.     (setf (block-succ block) ())
  570.     (setf (block-last block) node)
  571.     (link-blocks block new-block)
  572.     (add-to-dfo new-block block)
  573.     (setf (component-reanalyze (block-component block)) t)
  574.     
  575.     (do ((cont start (node-cont (continuation-next cont))))
  576.         ((eq cont last-cont)
  577.          (when (eq (continuation-kind last-cont) :inside-block)
  578.            (setf (continuation-block last-cont) new-block)))
  579.       (setf (continuation-block cont) new-block))
  580.  
  581.     (setf (block-type-asserted block) t)
  582.     (setf (block-test-modified block) t))))
  583.  
  584.   (undefined-value))
  585.  
  586.  
  587. ;;;; Deleting stuff:
  588.  
  589. ;;; Delete-Lambda-Var  --  Internal
  590. ;;;
  591. ;;;    Deal with deleting the last (read) reference to a lambda-var.  We
  592. ;;; iterate over all local calls flushing the corresponding argument, allowing
  593. ;;; the computation of the argument to be deleted.  We also mark the let for
  594. ;;; reoptimization, since it may be that we have deleted the last variable.
  595. ;;;
  596. ;;;    The lambda-var may still have some sets, but this doesn't cause too much
  597. ;;; difficulty, since we can efficiently implement write-only variables.  We
  598. ;;; iterate over the sets, marking their blocks for dead code flushing, since
  599. ;;; we can delete sets whose value is unused.
  600. ;;;
  601. (defun delete-lambda-var (leaf)
  602.   (declare (type lambda-var leaf))
  603.   (let* ((fun (lambda-var-home leaf))
  604.      (n (position leaf (lambda-vars fun))))
  605.     (dolist (ref (leaf-refs fun))
  606.       (let* ((cont (node-cont ref))
  607.          (dest (continuation-dest cont)))
  608.     (when (and (combination-p dest)
  609.            (eq (basic-combination-fun dest) cont)
  610.            (eq (basic-combination-kind dest) :local))
  611.       (let* ((args (basic-combination-args dest))
  612.          (arg (elt args n)))
  613.         (reoptimize-continuation arg)
  614.         (flush-dest arg)
  615.         (setf (elt args n) nil))))))
  616.  
  617.   (dolist (set (lambda-var-sets leaf))
  618.     (setf (block-flush-p (node-block set)) t))
  619.  
  620.   (undefined-value))
  621.  
  622.  
  623. ;;; REOPTIMIZE-LAMBDA-VAR  --  Internal
  624. ;;;
  625. ;;;    Note that something interesting has happened to Var.  We only deal with
  626. ;;; LET variables, marking the corresponding initial value arg as needing to be
  627. ;;; reoptimized.
  628. ;;;
  629. (defun reoptimize-lambda-var (var)
  630.   (declare (type lambda-var var))
  631.   (let ((fun (lambda-var-home var)))
  632.     (when (and (eq (functional-kind fun) :let)
  633.            (leaf-refs var))
  634.       (reoptimize-continuation
  635.        (elt (basic-combination-args
  636.          (continuation-dest
  637.           (node-cont
  638.            (first (leaf-refs fun)))))
  639.         (position var (lambda-vars fun))))))
  640.   (undefined-value))
  641.  
  642.  
  643. ;;; DELETE-FUNCTIONAL  --  Interface
  644. ;;;
  645. ;;;    This function deletes functions that have no references.  This need only
  646. ;;; be called on functions that never had any references, since otherwise
  647. ;;; DELETE-REF will handle the deletion. 
  648. ;;;
  649. (defun delete-functional (fun)
  650.   (assert (and (null (leaf-refs fun))
  651.            (not (functional-entry-function fun))))
  652.   (etypecase fun
  653.     (optional-dispatch (delete-optional-dispatch fun))
  654.     (clambda (delete-lambda fun)))
  655.   (undefined-value))
  656.  
  657.  
  658. ;;; MAYBE-REMOVE-FREE-FUNCTION  --  Interface
  659. ;;;
  660. ;;;    This function is called when we let convert a function or blow away an
  661. ;;; XEP, or otherwise do something that should prevent any new references to
  662. ;;; Fun (or its optional-dispatch) from being created.
  663. ;;;
  664. (defun maybe-remove-free-function (fun)
  665.   (declare (type functional fun))
  666.   (let* ((fun (etypecase fun
  667.         (clambda (or (lambda-optional-dispatch fun) fun))
  668.         (optional-dispatch fun)))
  669.      (entry (gethash (leaf-name fun) *free-functions*)))
  670.     (when (eq entry fun)
  671.       (remhash (leaf-name fun) *free-functions*)))
  672.   (undefined-value))
  673.  
  674. ;;; Delete-Lambda  --  Internal
  675. ;;;
  676. ;;;    Deal with deleting the last reference to a lambda.  Since there is only
  677. ;;; one way into a lambda, deleting the last reference to a lambda ensures that
  678. ;;; there is no way to reach any of the code in it.  So we just set the
  679. ;;; Functional-Kind for Fun and its Lets to :Deleted, causing IR1 optimization
  680. ;;; to delete blocks in that lambda.
  681. ;;;
  682. ;;;    If the function isn't a Let, we unlink the function head and tail from
  683. ;;; the component head and tail to indicate that the code is unreachable.  We
  684. ;;; also delete the function from Component-Lambdas (it won't be there before
  685. ;;; local call analysis, but no matter.)  If the lambda was never referenced,
  686. ;;; we give a note.
  687. ;;;
  688. ;;;    If the lambda is an XEP, then we null out the Entry-Function in its
  689. ;;; Entry-Function so that people will know that it is not an entry point
  690. ;;; anymore.
  691. ;;;
  692. (defun delete-lambda (leaf)
  693.   (declare (type clambda leaf))
  694.   (let ((kind (functional-kind leaf))
  695.     (bind (lambda-bind leaf)))
  696.     (assert (not (member kind '(:deleted :optional :top-level))))
  697.     (setf (functional-kind leaf) :deleted)
  698.     (setf (lambda-bind leaf) nil)
  699.     (dolist (let (lambda-lets leaf))
  700.       (setf (lambda-bind let) nil)
  701.       (setf (functional-kind let) :deleted))
  702.  
  703.     (if (member kind '(:let :mv-let :assignment))
  704.     (let ((home (lambda-home leaf)))
  705.       (setf (lambda-lets home) (delete leaf (lambda-lets home))))
  706.     (let* ((bind-block (node-block bind))
  707.            (component (block-component bind-block))
  708.            (return (lambda-return leaf)))
  709.       (assert (null (leaf-refs leaf)))
  710.       (unless (leaf-ever-used leaf)
  711.         (let ((*compiler-error-context* bind))
  712.           (compiler-note "Deleting unused function~:[.~;~:*~%  ~S~]"
  713.                  (leaf-name leaf))))
  714.       (unlink-blocks (component-head component) bind-block)
  715.       (when return
  716.         (unlink-blocks (node-block return) (component-tail component)))
  717.       (setf (component-reanalyze component) t)
  718.       (let ((tails (lambda-tail-set leaf)))
  719.         (setf (tail-set-functions tails)
  720.           (delete leaf (tail-set-functions tails)))
  721.         (setf (lambda-tail-set leaf) nil))
  722.       (setf (component-lambdas component)
  723.         (delete leaf (component-lambdas component)))))
  724.  
  725.     (when (eq kind :external)
  726.       (let ((fun (functional-entry-function leaf)))
  727.     (setf (functional-entry-function fun) nil)
  728.     (when (optional-dispatch-p fun)
  729.       (delete-optional-dispatch fun)))))
  730.  
  731.   (undefined-value))
  732.  
  733.  
  734. ;;; Delete-Optional-Dispatch  --  Internal
  735. ;;;
  736. ;;;    Deal with deleting the last reference to an Optional-Dispatch.  We have
  737. ;;; to be a bit more careful than with lambdas, since Delete-Ref is used both
  738. ;;; before and after local call analysis.  Afterward, all references to
  739. ;;; still-existing optional-dispatches have been moved to the XEP, leaving it
  740. ;;; with no references at all.  So we look at the XEP to see if an
  741. ;;; optional-dispatch is still really being used.  But before local call
  742. ;;; analysis, there are no XEPs, and all references are direct.
  743. ;;;
  744. ;;;    When we do delete the optional-dispatch, we grovel all of its
  745. ;;; entry-points, making them be normal lambdas, and then deleting the ones
  746. ;;; with no references.  This deletes any e-p lambdas that were either never
  747. ;;; referenced, or couldn't be deleted when the last deference was deleted (due
  748. ;;; to their :Optional kind.)
  749. ;;;
  750. ;;; Note that the last optional ep may alias the main entry, so when we process
  751. ;;; the main entry, its kind may have been changed to NIL or even converted to
  752. ;;; a let.
  753. ;;;
  754. (defun delete-optional-dispatch (leaf)
  755.   (declare (type optional-dispatch leaf))
  756.   (maybe-remove-free-function leaf)
  757.   (let ((entry (functional-entry-function leaf)))
  758.     (unless (and entry (leaf-refs entry))
  759.       (assert (or (not entry) (eq (functional-kind entry) :deleted)))
  760.       (setf (functional-kind leaf) :deleted)
  761.  
  762.       (flet ((frob (fun)
  763.            (unless (eq (functional-kind fun) :deleted)
  764.          (assert (eq (functional-kind fun) :optional))
  765.          (setf (functional-kind fun) nil)
  766.          (let ((refs (leaf-refs fun)))
  767.            (cond ((null refs)
  768.               (delete-lambda fun))
  769.              ((null (rest refs))
  770.               (or (maybe-let-convert fun)
  771.                   (maybe-convert-to-assignment fun)))
  772.              (t
  773.               (maybe-convert-to-assignment fun)))))))
  774.     
  775.     (dolist (ep (optional-dispatch-entry-points leaf))
  776.       (frob ep))
  777.     (when (optional-dispatch-more-entry leaf)
  778.       (frob (optional-dispatch-more-entry leaf)))
  779.     (let ((main (optional-dispatch-main-entry leaf)))
  780.       (when (eq (functional-kind main) :optional)
  781.         (frob main))))))
  782.  
  783.   (undefined-value))
  784.  
  785.  
  786. ;;; Delete-Ref  --  Interface
  787. ;;;
  788. ;;;    Do stuff to delete the semantic attachments of a Ref node.  When this
  789. ;;; leaves zero or one reference, we do a type dispatch off of the leaf to
  790. ;;; determine if a special action is appropriate.
  791. ;;;
  792. (defun delete-ref (ref)
  793.   (declare (type ref ref))
  794.   (let* ((leaf (ref-leaf ref))
  795.      (refs (delete ref (leaf-refs leaf))))
  796.     (setf (leaf-refs leaf) refs)
  797.     
  798.     (cond ((null refs)
  799.        (typecase leaf
  800.          (lambda-var (delete-lambda-var leaf))
  801.          (clambda
  802.           (ecase (functional-kind leaf)
  803.         ((nil :let :mv-let :assignment :escape :cleanup)
  804.          (assert (not (functional-entry-function leaf)))
  805.          (delete-lambda leaf))
  806.         (:external
  807.          (delete-lambda leaf))
  808.         ((:deleted :optional))))
  809.          (optional-dispatch
  810.           (unless (eq (functional-kind leaf) :deleted)
  811.         (delete-optional-dispatch leaf)))))
  812.       ((null (rest refs))
  813.        (typecase leaf
  814.          (clambda (or (maybe-let-convert leaf)
  815.               (maybe-convert-to-assignment leaf)))
  816.          (lambda-var (reoptimize-lambda-var leaf))))
  817.       (t
  818.        (typecase leaf
  819.          (clambda (maybe-convert-to-assignment leaf))))))
  820.  
  821.   (undefined-value))
  822.  
  823.  
  824. ;;; Delete-Return  --  Interface
  825. ;;;
  826. ;;;    Do stuff to indicate that the return node Node is being deleted.  We set
  827. ;;; the RETURN to NIL.
  828. ;;;
  829. (defun delete-return (node)
  830.   (declare (type creturn node))
  831.   (let ((fun (return-lambda node)))
  832.     (assert (lambda-return fun))
  833.     (setf (lambda-return fun) nil))
  834.   (undefined-value))
  835.  
  836.  
  837. ;;; NOTE-UNREFERENCED-VARS  --  Interface
  838. ;;;
  839. ;;;    If any of the Vars in fun were never referenced and was not declared
  840. ;;; IGNORE, then complain.
  841. ;;;
  842. (defun note-unreferenced-vars (fun)
  843.   (declare (type clambda fun))
  844.   (dolist (var (lambda-vars fun))
  845.     (unless (or (leaf-ever-used var)
  846.         (lambda-var-ignorep var))
  847.       (let ((*compiler-error-context* (lambda-bind fun)))
  848.     (unless (policy *compiler-error-context* (= brevity 3))
  849.       (compiler-warning "Variable ~S defined but never used."
  850.                 (leaf-name var)))
  851.     (setf (leaf-ever-used var) t))))
  852.   (undefined-value))
  853.  
  854.  
  855. ;;; Flush-Dest  --  Interface
  856. ;;;
  857. ;;;    This function is called by people who delete nodes; it provides a way to
  858. ;;; indicate that the value of a continuation is no longer used.  We null out
  859. ;;; the Continuation-Dest, set Flush-P in the blocks containing uses of Cont
  860. ;;; and set Component-Reoptimize.  If the Prev of the use is deleted, then we
  861. ;;; blow off reoptimization.
  862. ;;;
  863. ;;;    If the continuation is :Deleted, then we don't do anything, since all
  864. ;;; semantics have already been flushed.  :Deleted-Block-Start start
  865. ;;; continuations are treated just like :Block-Start; it is possible that the
  866. ;;; continuation may be given a new dest (e.g. by SUBSTITUTE-CONTINUATION), so
  867. ;;; we don't want to delete it.
  868. ;;;
  869. (defun flush-dest (cont)
  870.   (declare (type continuation cont))
  871.   
  872.   (unless (eq (continuation-kind cont) :deleted)
  873.     (assert (continuation-dest cont))
  874.     (setf (continuation-dest cont) nil)
  875.     (do-uses (use cont)
  876.       (let ((prev (node-prev use)))
  877.     (unless (eq (continuation-kind prev) :deleted)
  878.       (let ((block (continuation-block prev)))
  879.         (setf (component-reoptimize (block-component block)) t)
  880.         (setf (block-attributep (block-flags block) flush-p type-asserted)
  881.           t))))))
  882.  
  883.   (setf (continuation-%type-check cont) nil)
  884.   
  885.   (undefined-value))
  886.  
  887.  
  888. ;;; MARK-FOR-DELETION  --  Internal
  889. ;;;
  890. ;;;    Do a graph walk backward from Block, marking all predecessor blocks with
  891. ;;; the DELETE-P flag.
  892. ;;;
  893. (defun mark-for-deletion (block)
  894.   (declare (type cblock block))
  895.   (unless (block-delete-p block)
  896.     (setf (block-delete-p block) t)
  897.     (setf (component-reanalyze (block-component block)) t)
  898.     (dolist (pred (block-pred block))
  899.       (mark-for-deletion pred)))
  900.   (undefined-value))
  901.  
  902.  
  903. ;;; DELETE-CONTINUATION  --  Interface
  904. ;;;
  905. ;;;    Delete Cont, eliminating both control and value semantics.  We set
  906. ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST.  Here we must
  907. ;;; get the component from the use block, since the continuation may be a
  908. ;;; :DELETED-BLOCK-START.
  909. ;;;
  910. ;;;    If Cont has DEST, then it must be the case that the DEST is unreachable,
  911. ;;; since we can't compute the value desired.  In this case, we call
  912. ;;; MARK-FOR-DELETION to cause the DEST block and its predecessors to tell
  913. ;;; people to ignore them, and to cause them to be deleted eventually.
  914. ;;;
  915. (defun delete-continuation (cont)
  916.   (declare (type continuation cont))
  917.   (assert (not (eq (continuation-kind cont) :deleted)))
  918.   
  919.   (do-uses (use cont)
  920.     (let ((prev (node-prev use)))
  921.       (unless (eq (continuation-kind prev) :deleted)
  922.     (let ((block (continuation-block prev)))
  923.       (setf (block-attributep (block-flags block) flush-p type-asserted) t)
  924.       (setf (component-reoptimize (block-component block)) t)))))
  925.  
  926.   (let ((dest (continuation-dest cont)))
  927.     (when dest
  928.       (let ((prev (node-prev dest)))
  929.     (when (and prev
  930.            (not (eq (continuation-kind prev) :deleted)))
  931.       (let ((block (continuation-block prev)))
  932.         (unless (block-delete-p block)
  933.           (mark-for-deletion block)))))))
  934.   
  935.   (setf (continuation-kind cont) :deleted)
  936.   (setf (continuation-dest cont) nil)
  937.   (setf (continuation-next cont) nil)
  938.   (setf (continuation-asserted-type cont) *empty-type*)
  939.   (setf (continuation-%derived-type cont) *empty-type*)
  940.   (setf (continuation-use cont) nil)
  941.   (setf (continuation-block cont) nil)
  942.   (setf (continuation-reoptimize cont) nil)
  943.   (setf (continuation-%type-check cont) nil)
  944.   (setf (continuation-info cont) nil)
  945.   
  946.   (undefined-value))
  947.  
  948.  
  949. (defvar *deletion-ignored-objects* '(t nil))
  950.  
  951. ;;; PRESENT-IN-FORM  --  Internal
  952. ;;;
  953. ;;;    Return true if we can find Obj in Form, NIL otherwise.  We bound our
  954. ;;; recursion so that we don't get lost in circular structures.  We ignore the
  955. ;;; car of forms if they are a symbol (to prevent confusing function
  956. ;;; referencess with variables), and we also ignore anything inside ' or #'.
  957. ;;;
  958. (defun present-in-form (obj form depth)
  959.   (declare (type (integer 0 20) depth))
  960.   (cond ((= depth 20) nil)
  961.     ((eq obj form) t)
  962.     ((atom form) nil)
  963.     (t
  964.      (let ((first (car form))
  965.            (depth (1+ depth)))
  966.        (if (member first '(quote function))
  967.            nil
  968.            (or (and (not (symbolp first))
  969.             (present-in-form obj first depth))
  970.            (do ((l (cdr form) (cdr l))
  971.             (n 0 (1+ n)))
  972.                ((or (atom l) (> n 100))
  973.             nil)
  974.              (declare (fixnum n))
  975.              (when (present-in-form obj (car l) depth)
  976.                (return t)))))))))
  977.  
  978.  
  979. ;;; NOTE-BLOCK-DELETION  --  Internal
  980. ;;;
  981. ;;;    This function is called on a block immediately before we delete it.  We
  982. ;;; check to see if any of the code about to die appeared in the original
  983. ;;; source, and emit a note if so.
  984. ;;;
  985. ;;;    If the block was in a lambda is now deleted, then we ignore the whole
  986. ;;; block, since this case is picked off in DELETE-LAMBDA.  We also ignore the
  987. ;;; deletion of CRETURN nodes, since it is somewhat reasonable for a function
  988. ;;; to not return, and there is a different note for that case anyway.
  989. ;;;
  990. ;;;    If the actual source is an atom, then we use a bunch of heuristics to
  991. ;;; guess whether this reference really appeared in the original source:
  992. ;;; -- If a symbol, it must be interned and not a keyword.
  993. ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum or a
  994. ;;;    character.)
  995. ;;; -- The atom must be "present" in the original source form, and present in
  996. ;;;    all intervening actual source forms.
  997. ;;;
  998. (defun note-block-deletion (block)
  999.   (let ((home (block-home-lambda block)))
  1000.     (unless (eq (functional-kind home) :deleted)
  1001.       (do-nodes (node cont block)
  1002.     (let* ((path (node-source-path node))
  1003.            (first (first path)))
  1004.       (when (or (eq first 'original-source-start)
  1005.             (and (atom first)
  1006.              (or (not (symbolp first))
  1007.                  (let ((pkg (symbol-package first)))
  1008.                    (and pkg
  1009.                     (not (eq pkg (symbol-package :end))))))
  1010.              (not (member first *deletion-ignored-objects*))
  1011.              (not (typep first '(or fixnum character)))
  1012.              (every #'(lambda (x)
  1013.                     (present-in-form first x 0))
  1014.                 (source-path-forms path))
  1015.              (present-in-form first (find-original-source path)
  1016.                       0)))
  1017.         (unless (return-p node)
  1018.           (let ((*compiler-error-context* node))
  1019.         (compiler-note "Deleting unreachable code.")))
  1020.         (return))))))
  1021.   (undefined-value))
  1022.  
  1023.  
  1024. ;;; Delete-Block  --  Interface
  1025. ;;;
  1026. ;;;    This function does what is necessary to eliminate the code in it from
  1027. ;;; the IR1 representation.  This involves unlinking it from its predecessors
  1028. ;;; and successors and deleting various node-specific semantic information.
  1029. ;;;
  1030. ;;;    We mark the Start as has having no next and remove the last node from
  1031. ;;; its Cont's uses.  We also flush the DEST for all continuations whose values
  1032. ;;; are received by nodes in the block.
  1033. ;;;
  1034. (defun delete-block (block)
  1035.   (declare (type cblock block))
  1036.   (assert (block-component block) () "Block is already deleted.")
  1037.   (note-block-deletion block)
  1038.   (setf (block-delete-p block) t)
  1039.  
  1040.   (let* ((last (block-last block))
  1041.      (cont (node-cont last)))
  1042.     (delete-continuation-use last)
  1043.     (if (eq (continuation-kind cont) :unused)
  1044.     (delete-continuation cont)
  1045.     (reoptimize-continuation cont)))
  1046.  
  1047.   (dolist (b (block-pred block))
  1048.     (unlink-blocks b block))
  1049.   (dolist (b (block-succ block))
  1050.     (unlink-blocks block b))
  1051.  
  1052.   (do-nodes (node cont block)
  1053.     (typecase node
  1054.       (ref (delete-ref node))
  1055.       (cif
  1056.        (flush-dest (if-test node)))
  1057.       ;;
  1058.       ;; The next two cases serve to maintain the invariant that a LET always
  1059.       ;; has a well-formed COMBINATION, REF and BIND.  We delete the lambda
  1060.       ;; whenever we delete any of these, but we must be careful that this LET
  1061.       ;; has not already been partially deleted.
  1062.       (basic-combination
  1063.        (when (and (eq (basic-combination-kind node) :local)
  1064.           ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
  1065.           (continuation-use (basic-combination-fun node)))
  1066.      (let ((fun (combination-lambda node)))
  1067.        ;; If our REF was the 2'nd to last ref, and has been deleted, then
  1068.        ;; Fun may be a LET for some other combination.
  1069.        (when (and (member (functional-kind fun) '(:let :mv-let))
  1070.               (eq (let-combination fun) node))
  1071.          (delete-lambda fun))))
  1072.        (flush-dest (basic-combination-fun node))
  1073.        (dolist (arg (basic-combination-args node))
  1074.      (when arg (flush-dest arg))))
  1075.       (bind
  1076.        (let ((lambda (bind-lambda node)))
  1077.      (unless (eq (functional-kind lambda) :deleted)
  1078.        (assert (member (functional-kind lambda)
  1079.                '(:let :mv-let :assignment)))
  1080.        (delete-lambda lambda))))
  1081.       (exit
  1082.        (let ((value (exit-value node))
  1083.          (entry (exit-entry node)))
  1084.      (when value
  1085.        (flush-dest value))
  1086.      (when entry
  1087.        (setf (entry-exits entry)
  1088.          (delete node (entry-exits entry))))))
  1089.       (creturn
  1090.        (flush-dest (return-result node))
  1091.        (delete-return node))
  1092.       (cset
  1093.        (flush-dest (set-value node))
  1094.        (let ((var (set-var node)))
  1095.      (setf (basic-var-sets var)
  1096.            (delete node (basic-var-sets var))))))
  1097.  
  1098.     (delete-continuation (node-prev node)))
  1099.  
  1100.   (remove-from-dfo block)
  1101.   (undefined-value))
  1102.  
  1103.  
  1104. ;;; Unlink-Node  --  Interface
  1105. ;;;
  1106. ;;;    Delete a node from a block, deleting the block if there are no nodes
  1107. ;;; left.  We remove the node from the uses of its CONT, but we don't deal with
  1108. ;;; cleaning up any type-specific semantic attachments.  If the CONT is :UNUSED
  1109. ;;; after deleting this use, then we delete CONT.  (Note :UNUSED is not the
  1110. ;;; same as no uses.  A continuation will only become :UNUSED if it was
  1111. ;;; :INSIDE-BLOCK before.) 
  1112. ;;;
  1113. ;;;    If the node is the last node, there must be exactly one successor.  We
  1114. ;;; link all of our precedessors to the successor and unlink the block.  In
  1115. ;;; this case, we return T, otherwise NIL.  If no nodes are left, and the block
  1116. ;;; is a successor of itself, then we replace the only node with a degenerate
  1117. ;;; exit node.  This provides a way to represent the bodyless infinite loop,
  1118. ;;; given the prohibition on empty blocks in IR1.
  1119. ;;;
  1120. (defun unlink-node (node)
  1121.   (declare (type node node))
  1122.   (let* ((cont (node-cont node))
  1123.      (next (continuation-next cont))
  1124.      (prev (node-prev node))
  1125.      (block (continuation-block prev))
  1126.      (prev-kind (continuation-kind prev))
  1127.      (last (block-last block)))
  1128.     
  1129.     (unless (eq (continuation-kind cont) :deleted)
  1130.       (delete-continuation-use node)
  1131.       (when (eq (continuation-kind cont) :unused)
  1132.     (assert (not (continuation-dest cont)))
  1133.     (delete-continuation cont)))
  1134.     
  1135.     (setf (block-type-asserted block) t)
  1136.     (setf (block-test-modified block) t)
  1137.  
  1138.     (cond ((or (eq prev-kind :inside-block)
  1139.            (and (eq prev-kind :block-start)
  1140.             (not (eq node last))))
  1141.        (cond ((eq node last)
  1142.           (setf (block-last block) (continuation-use prev))
  1143.           (setf (continuation-next prev) nil))
  1144.          (t
  1145.           (setf (continuation-next prev) next)
  1146.           (setf (node-prev next) prev)))
  1147.        (setf (node-prev node) nil)
  1148.        nil)
  1149.       (t
  1150.        (assert (eq prev-kind :block-start))
  1151.        (assert (eq node last))
  1152.        (let* ((succ (block-succ block))
  1153.           (next (first succ)))
  1154.          (assert (and succ (null (cdr succ))))
  1155.          (cond
  1156.           ((member block succ)
  1157.            (with-ir1-environment node
  1158.          (let ((exit (make-exit))
  1159.                (dummy (make-continuation)))
  1160.            (setf (continuation-next prev) nil)
  1161.            (prev-link exit prev)
  1162.            (add-continuation-use exit dummy)
  1163.            (setf (block-last block) exit)))
  1164.            (setf (node-prev node) nil)
  1165.            nil)
  1166.           (t
  1167.            (assert (eq (block-start-cleanup block)
  1168.                (block-end-cleanup block)))
  1169.            (unlink-blocks block next)
  1170.            (dolist (pred (block-pred block))
  1171.          (change-block-successor pred block next))
  1172.            (remove-from-dfo block)
  1173.            (cond ((continuation-dest prev)
  1174.               (setf (continuation-next prev) nil)
  1175.               (setf (continuation-kind prev) :deleted-block-start))
  1176.              (t
  1177.               (delete-continuation prev)))
  1178.            (setf (node-prev node) nil)
  1179.            t)))))))
  1180.  
  1181.  
  1182. ;;; NODE-DELETED  --  Interface
  1183. ;;;
  1184. ;;;    Return true if NODE has been deleted, false if it is still a valid part
  1185. ;;; of IR1.
  1186. ;;;
  1187. (defun node-deleted (node)
  1188.   (declare (type node node))
  1189.   (let ((prev (node-prev node)))
  1190.     (not (and prev
  1191.           (not (eq (continuation-kind prev) :deleted))
  1192.           (let ((block (continuation-block prev)))
  1193.         (and (block-component block)
  1194.              (not (block-delete-p block))))))))
  1195.  
  1196.  
  1197. ;;; DELETE-COMPONENT  --  Interface
  1198. ;;;
  1199. ;;;    Delete all the blocks and functions in Component.  We scan first marking
  1200. ;;; the blocks as delete-p to prevent weird stuff from being triggered by
  1201. ;;; deletion.
  1202. ;;;
  1203. (defun delete-component (component)
  1204.   (declare (type component component))
  1205.   (assert (null (component-new-functions component)))
  1206.   (setf (component-kind component) :deleted)
  1207.   (do-blocks (block component)
  1208.     (setf (block-delete-p block) t))
  1209.   (dolist (fun (component-lambdas component))
  1210.     (setf (functional-kind fun) nil)
  1211.     (setf (leaf-refs fun) nil)
  1212.     (delete-lambda fun))
  1213.   (do-blocks (block component)
  1214.     (delete-block block))
  1215.   (undefined-value))
  1216.   
  1217.  
  1218. ;;; EXTRACT-FUNCTION-ARGS -- interface
  1219. ;;;
  1220. ;;; Convert code of the form (foo ... (fun ...) ...) to (foo ... ... ...).
  1221. ;;; In other words, replace the function combination fun by it's arguments.
  1222. ;;; If there are any problems with doing this, use GIVE-UP to blow out of
  1223. ;;; whatever transform called this.  Note, as the number of arguments changes,
  1224. ;;; the transform must be prepared to return a lambda with a new lambda-list
  1225. ;;; with the correct number of arguments.
  1226. ;;; 
  1227. (defun extract-function-args (cont fun num-args)
  1228.   "If CONT is a call to FUN with NUM-ARGS args, change those arguments
  1229.    to feed directly to the continuation-dest of CONT, which must be
  1230.    a combination."
  1231.   (declare (type continuation cont)
  1232.        (type symbol fun)
  1233.        (type index num-args))
  1234.   (let ((outside (continuation-dest cont))
  1235.     (inside (continuation-use cont)))
  1236.     (assert (combination-p outside))
  1237.     (unless (combination-p inside)
  1238.       (give-up))
  1239.     (let ((inside-fun (combination-fun inside)))
  1240.       (unless (eq (continuation-function-name inside-fun) fun)
  1241.     (give-up))
  1242.       (let ((inside-args (combination-args inside)))
  1243.     (unless (= (length inside-args) num-args)
  1244.       (give-up))
  1245.     (let* ((outside-args (combination-args outside))
  1246.            (arg-position (position cont outside-args))
  1247.            (before-args (subseq outside-args 0 arg-position))
  1248.            (after-args (subseq outside-args (1+ arg-position))))
  1249.       (dolist (arg inside-args)
  1250.         (setf (continuation-dest arg) outside))
  1251.       (setf (combination-args inside) nil)
  1252.       (setf (combination-args outside)
  1253.         (append before-args inside-args after-args))
  1254.       (change-ref-leaf (continuation-use inside-fun)
  1255.                (find-free-function 'list "???"))
  1256.       (setf (combination-kind inside) :full)
  1257.       (setf (node-derived-type inside) *wild-type*)
  1258.       (flush-dest cont)
  1259.       (setf (continuation-asserted-type cont) *wild-type*)
  1260.       (undefined-value))))))
  1261.  
  1262.  
  1263.  
  1264. ;;;; Leaf hackery:
  1265.  
  1266. ;;; Change-Ref-Leaf  --  Interface
  1267. ;;;
  1268. ;;;    Change the Leaf that a Ref refers to.
  1269. ;;;
  1270. (defun change-ref-leaf (ref leaf)
  1271.   (declare (type ref ref) (type leaf leaf))
  1272.   (unless (eq (ref-leaf ref) leaf)
  1273.     (push ref (leaf-refs leaf))
  1274.     (delete-ref ref)
  1275.     (setf (ref-leaf ref) leaf)
  1276.     (let ((ltype (leaf-type leaf)))
  1277.       (if (function-type-p ltype)
  1278.       (setf (node-derived-type ref) ltype)
  1279.       (derive-node-type ref ltype)))
  1280.     (reoptimize-continuation (node-cont ref)))
  1281.   (undefined-value))
  1282.  
  1283.  
  1284. ;;; Substitute-Leaf  --  Interface
  1285. ;;;
  1286. ;;;    Change all Refs for Old-Leaf to New-Leaf.
  1287. ;;;
  1288. (defun substitute-leaf (new-leaf old-leaf)
  1289.   (declare (type leaf new-leaf old-leaf))
  1290.   (dolist (ref (leaf-refs old-leaf))
  1291.     (change-ref-leaf ref new-leaf))
  1292.   (undefined-value))
  1293.  
  1294. ;;; SUBSTITUTE-LEAF-IF  --  Interface
  1295. ;;;
  1296. ;;;    Like SUBSITIUTE-LEAF, only there is a predicate on the Ref to tell
  1297. ;;; whether to substitute.
  1298. ;;;
  1299. (defun substitute-leaf-if (test new-leaf old-leaf)
  1300.   (declare (type leaf new-leaf old-leaf) (type function test))
  1301.   (dolist (ref (leaf-refs old-leaf))
  1302.     (when (funcall test ref)
  1303.       (change-ref-leaf ref new-leaf)))
  1304.   (undefined-value))
  1305.  
  1306. ;;; Find-Constant  --  Interface
  1307. ;;;
  1308. ;;;    Return a Leaf which represents the specified constant object.  If the
  1309. ;;; object is not in *constants*, then we create a new constant Leaf and
  1310. ;;; enter it.
  1311. ;;;
  1312. (defun find-constant (object)
  1313.   (or (gethash object *constants*)
  1314.       (setf (gethash object *constants*)
  1315.         (make-constant :value object  :name nil
  1316.                :type (ctype-of object)
  1317.                :where-from :defined))))
  1318.  
  1319.  
  1320. ;;;; Find-NLX-Info  --  Interface
  1321. ;;;
  1322. ;;;    If there is a non-local exit noted in Entry's environment that exits to
  1323. ;;; Cont in that entry, then return it, otherwise return NIL.
  1324. ;;;
  1325. (defun find-nlx-info (entry cont)
  1326.   (declare (type entry entry) (type continuation cont))
  1327.   (let ((entry-cleanup (entry-cleanup entry)))
  1328.     (dolist (nlx (environment-nlx-info (node-environment entry)) nil)
  1329.       (when (and (eq (nlx-info-continuation nlx) cont)
  1330.          (eq (nlx-info-cleanup nlx) entry-cleanup))
  1331.     (return nlx)))))
  1332.  
  1333.  
  1334. ;;;; Functional hackery:
  1335.  
  1336. ;;; Main-Entry  --  Interface
  1337. ;;;
  1338. ;;;    If Functional is a Lambda, just return it; if it is an
  1339. ;;; optional-dispatch, return the main-entry.
  1340. ;;;
  1341. (proclaim '(function main-entry (functional) clambda))
  1342. (defun main-entry (functional)
  1343.   (if (lambda-p functional)
  1344.       functional
  1345.       (optional-dispatch-main-entry functional)))
  1346.  
  1347. ;;; Looks-Like-An-MV-Bind  --  Interface
  1348. ;;;
  1349. ;;;    Returns true if Functional is a thing that can be treated like MV-Bind
  1350. ;;; when it appears in an MV-Call.  All fixed arguments must be optional with
  1351. ;;; null default and no supplied-p.  There must be a rest arg with no
  1352. ;;; references.
  1353. ;;;
  1354. (proclaim '(function looks-like-an-mv-bind (functional) boolean))
  1355. (defun looks-like-an-mv-bind (functional)
  1356.   (and (optional-dispatch-p functional)
  1357.        (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
  1358.        ((null arg) nil)
  1359.      (let ((info (lambda-var-arg-info (car arg))))
  1360.        (unless info (return nil))
  1361.        (case (arg-info-kind info)
  1362.          (:optional
  1363.           (when (or (arg-info-supplied-p info) (arg-info-default info))
  1364.         (return nil)))
  1365.          (:rest
  1366.           (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
  1367.          (t
  1368.           (return nil)))))))
  1369.  
  1370. ;;; External-Entry-Point-P  --  Interface
  1371. ;;;
  1372. ;;;    Return true if function is an XEP.  This is true of normal XEPs
  1373. ;;; (:External kind) and top-level lambdas (:Top-Level kind.)
  1374. ;;;
  1375. (defun external-entry-point-p (fun)
  1376.   (declare (type functional fun))
  1377.   (not (null (member (functional-kind fun) '(:external :top-level)))))
  1378.  
  1379.  
  1380. ;;; Continuation-Function-Name  --  Interface
  1381. ;;;
  1382. ;;;    If Cont's only use is a non-notinline global function reference, then
  1383. ;;; return the referenced symbol, otherwise NIL.  If Notinline-OK is true, then
  1384. ;;; we don't care if the ref is notinline.
  1385. ;;;
  1386. (defun continuation-function-name (cont &optional notinline-ok)
  1387.   (declare (type continuation cont))
  1388.   (let ((use (continuation-use cont)))
  1389.     (if (and (ref-p use)
  1390.          (or (not (eq (ref-inlinep use) :notinline))
  1391.          notinline-ok))
  1392.     (let ((leaf (ref-leaf use)))
  1393.       (if (and (global-var-p leaf)
  1394.            (eq (global-var-kind leaf) :global-function))
  1395.           (leaf-name leaf)
  1396.           nil))
  1397.     nil)))
  1398.  
  1399.  
  1400. ;;; LET-COMBINATION  --  Interface
  1401. ;;;
  1402. ;;;    Return the COMBINATION node that is the call to the let Fun.
  1403. ;;;
  1404. (defun let-combination (fun)
  1405.   (declare (type clambda fun))
  1406.   (assert (member (functional-kind fun) '(:let :mv-let)))
  1407.   (continuation-dest (node-cont (first (leaf-refs fun)))))
  1408.  
  1409.  
  1410. ;;; LET-VAR-INITIAL-VALUE  --  Interface
  1411. ;;;
  1412. ;;;    Return the initial value continuation for a let variable or NIL if none.
  1413. ;;;
  1414. (defun let-var-initial-value (var)
  1415.   (declare (type lambda-var var))
  1416.   (let ((fun (lambda-var-home var)))
  1417.     (elt (combination-args (let-combination fun))
  1418.      (position var (lambda-vars fun)))))
  1419.  
  1420.  
  1421. ;;; COMBINATION-LAMBDA  --  Interface
  1422. ;;;
  1423. ;;;    Return the LAMBDA that is called by the local Call.
  1424. ;;;
  1425. (defun combination-lambda (call)
  1426.   (declare (type basic-combination call))
  1427.   (assert (eq (basic-combination-kind call) :local))
  1428.   (ref-leaf (continuation-use (basic-combination-fun call))))
  1429.  
  1430.  
  1431. ;;;; Compiler error context determination:
  1432.  
  1433. (proclaim '(special *current-path*))
  1434.  
  1435.  
  1436. ;;; We bind print level and length when printing out messages so that we don't
  1437. ;;; dump huge amounts of garbage.
  1438. ;;;
  1439. (proclaim '(type (or unsigned-byte null) *error-print-level*
  1440.          *error-print-length* *error-print-lines*))
  1441.  
  1442. (defvar *error-print-level* 3
  1443.   "The value for *Print-Level* when printing compiler error messages.")
  1444. (defvar *error-print-length* 5
  1445.   "The value for *Print-Length* when printing compiler error messages.")
  1446. (defvar *error-print-lines* 5
  1447.   "The value for *Print-Lines* when printing compiler error messages.")
  1448.  
  1449. (defvar *enclosing-source-cutoff* 1
  1450.   "The maximum number of enclosing non-original source forms (i.e. from
  1451.   macroexpansion) that we print in full.  For additional enclosing forms, we
  1452.   print only the CAR.")
  1453. (proclaim '(type unsigned-byte *enclosing-source-cutoff*))
  1454.  
  1455.  
  1456. ;;; We separate the determination of compiler error contexts from the actual
  1457. ;;; signalling of those errors by objectifying the error context.  This allows
  1458. ;;; postponement of the determination of how (and if) to signal the error.
  1459. ;;;
  1460. ;;; We take care not to reference any of the IR1 so that pending potential
  1461. ;;; error messages won't prevent the IR1 from being GC'd.  To this end, we
  1462. ;;; convert source forms to strings so that source forms that contain IR1
  1463. ;;; references (e.g. %DEFUN) don't hold onto the IR.
  1464. ;;;
  1465. (defstruct (compiler-error-context
  1466.         (:print-function
  1467.          (lambda (s stream d)
  1468.            (declare (ignore s d))
  1469.            (format stream "#<Compiler-Error-Context>"))))
  1470.   ;;
  1471.   ;; A list of the stringified CARs of the enclosing non-original source forms
  1472.   ;; exceeding the *enclosing-source-cutoff*.
  1473.   (enclosing-source nil :type list)
  1474.   ;;
  1475.   ;; A list of stringified enclosing non-original source forms.
  1476.   (source nil :type list)
  1477.   ;;
  1478.   ;; The stringified form in the original source that expanded into Source.
  1479.   (original-source (required-argument) :type simple-string)
  1480.   ;;
  1481.   ;; A list of prefixes of "interesting" forms that enclose original-source.
  1482.   (context nil :type list)
  1483.   ;;
  1484.   ;; The FILE-INFO-NAME for the relevant FILE-INFO.
  1485.   (file-name (required-argument)
  1486.          :type (or simple-string (member :lisp :stream)))
  1487.   ;;
  1488.   ;; The file position at which the top-level form starts, if applicable.
  1489.   (file-position nil :type (or index null))
  1490.   ;;
  1491.   ;; The original source part of the source path.
  1492.   (original-source-path nil :type list))
  1493.   
  1494.   
  1495. ;;; If true, this is the node which is used as context in compiler warning
  1496. ;;; messages.
  1497. ;;;
  1498. (proclaim '(type (or null compiler-error-context node)
  1499.          *compiler-error-context*))
  1500. (defvar *compiler-error-context* nil)
  1501.  
  1502.  
  1503. ;;; Hashtable mapping macro names to source context parsers.  Each parser
  1504. ;;; function returns the source-context list for that form.
  1505. ;;; 
  1506. (defvar *source-context-methods* (make-hash-table))
  1507.  
  1508. ;;; DEF-SOURCE-CONTEXT  --  Public
  1509. ;;;
  1510. (defmacro def-source-context (name ll &body body)
  1511.   "DEF-SOURCE-CONTEXT Name Lambda-List Form*
  1512.    This macro defines how to extract an abbreviated source context from the
  1513.    Named form when it appears in the compiler input.  Lambda-List is a DEFMACRO
  1514.    style lambda-list used to parse the arguments.  The Body should return a
  1515.    list of subforms suitable for a \"~{~S ~}\" format string."
  1516.   (let ((n-whole (gensym)))
  1517.     `(setf (gethash ',name *source-context-methods*)
  1518.        #'(lambda (,n-whole)
  1519.            (destructuring-bind ,ll ,n-whole ,@body)))))
  1520.  
  1521. (def-source-context defstruct (name-or-options &rest slots)
  1522.   (declare (ignore slots))
  1523.   `(defstruct ,(if (consp name-or-options)
  1524.            (car name-or-options)
  1525.            name-or-options)))
  1526.  
  1527. (def-source-context function (thing)
  1528.   (if (and (consp thing) (eq (first thing) 'lambda) (consp (rest thing)))
  1529.       `(lambda ,(second thing))
  1530.       `(function ,thing)))
  1531.  
  1532. #+pcl
  1533. (def-source-context pcl::defmethod (name &rest stuff)
  1534.   (let ((arg-pos (position-if #'listp stuff)))
  1535.     (if arg-pos
  1536.     `(pcl::defmethod ,name ,@(subseq stuff 0 arg-pos)
  1537.        ,@(nth-value 2 (pcl::parse-specialized-lambda-list
  1538.                (elt stuff arg-pos))))
  1539.     `(pcl::defmethod ,name "<illegal syntax>"))))
  1540.  
  1541.  
  1542. ;;; SOURCE-FORM-CONTEXT  --  Internal
  1543. ;;;
  1544. ;;;    Return the first two elements of Form if Form is a list.  Take the car
  1545. ;;; of the second form if appropriate.
  1546. ;;;
  1547. (defun source-form-context (form)
  1548.   (cond ((atom form) nil)
  1549.     ((>= (length form) 2)
  1550.      (funcall (gethash (first form) *source-context-methods*
  1551.                #'(lambda (x)
  1552.                    (declare (ignore x))
  1553.                    (list (first form) (second form))))
  1554.           (rest form)))
  1555.     (t
  1556.      form)))
  1557.  
  1558.  
  1559. ;;; Find-Original-Source  --  Internal
  1560. ;;;
  1561. ;;;    Given a source path, return the original source form and a description
  1562. ;;; of the interesting aspects of the context in which it appeared.  The
  1563. ;;; context is a list of lists, one sublist per context form.  The sublist is a
  1564. ;;; list of some of the initial subforms of the context form.
  1565. ;;;
  1566. ;;; For now, we use the first two subforms of each interesting form.  A form is
  1567. ;;; interesting if the first element is a symbol beginning with "DEF" and it is
  1568. ;;; not the source form.  If there is no DEF-mumble, then we use the outermost
  1569. ;;; containing form.  If the second subform is a list, then in some cases we
  1570. ;;; return the car of that form rather than the whole form (i.e. don't show
  1571. ;;; defstruct options, etc.)
  1572. ;;;
  1573. (defun find-original-source (path)
  1574.   (declare (list path))
  1575.   (let* ((rpath (reverse (source-path-original-source path)))
  1576.      (tlf (first rpath))
  1577.      (root (find-source-root tlf *source-info*)))
  1578.     (collect ((context))
  1579.       (let ((form root)
  1580.         (current (rest rpath)))
  1581.     (loop
  1582.       (when (atom form)
  1583.         (assert (null current))
  1584.         (return))
  1585.       (let ((head (first form)))
  1586.         (when (symbolp head)
  1587.           (let ((name (symbol-name head)))
  1588.         (when (and (>= (length name) 3) (string= name "DEF" :end1 3))
  1589.           (context (source-form-context form))))))
  1590.       (when (null current) (return))
  1591.       (setq form (nth (pop current) form)))
  1592.     
  1593.     (cond ((context)
  1594.            (values form (context)))
  1595.           ((and path root)
  1596.            (let ((c (source-form-context root)))
  1597.          (values form (if c (list c) nil))))
  1598.           (t
  1599.            (values '(unable to locate source)
  1600.                '((some strange place)))))))))
  1601.  
  1602.  
  1603. ;;; STRINGIFY-FORM  --  Internal
  1604. ;;;
  1605. ;;;    Convert a source form to a string, formatted suitably for use in
  1606. ;;; compiler warnings.
  1607. ;;;
  1608. (defun stringify-form (form &optional (pretty t))
  1609.   (let ((*print-level* (or *error-print-level* *print-level*))
  1610.     (*print-length* (or *error-print-length* *print-length*))
  1611.     (*print-lines* (or *error-print-lines* *print-lines*))
  1612.     (*print-pretty* pretty))
  1613.     (if pretty
  1614.     (format nil "  ~S~%" form)
  1615.     (prin1-to-string form))))
  1616.  
  1617.       
  1618. ;;; FIND-ERROR-CONTEXT  --  Interface
  1619. ;;;
  1620. ;;;    Return a COMPILER-ERROR-CONTEXT structure describing the current error
  1621. ;;; context, or NIL if we can't figure anything out.  Args is a list of things
  1622. ;;; that are going to be printed out in the error message, and can thus be
  1623. ;;; blown off when they appear in the source context.
  1624. ;;;
  1625. (defun find-error-context (args)
  1626.   (let ((context *compiler-error-context*))
  1627.     (if (compiler-error-context-p context)
  1628.     context
  1629.     (let ((path (or *current-path*
  1630.             (if context
  1631.                 (node-source-path context)
  1632.                 nil))))
  1633.       (when (and *source-info* path)
  1634.         (multiple-value-bind (form src-context)
  1635.                  (find-original-source path)
  1636.           (collect ((full nil cons)
  1637.             (short nil cons))
  1638.         (let ((forms (source-path-forms path))
  1639.               (n 0))
  1640.           (dolist (src (if (member (first forms) args)
  1641.                    (rest forms)
  1642.                    forms))
  1643.             (if (>= n *enclosing-source-cutoff*)
  1644.             (short (stringify-form (if (consp src)
  1645.                            (car src)
  1646.                            src)
  1647.                            nil))
  1648.             (full (stringify-form src)))
  1649.             (incf n)))
  1650.  
  1651.         (let* ((tlf (source-path-tlf-number path))
  1652.                (file (find-file-info tlf *source-info*)))
  1653.           (make-compiler-error-context
  1654.            :enclosing-source (short)
  1655.            :source (full)
  1656.            :original-source (stringify-form form)
  1657.            :context src-context
  1658.            :file-name (file-info-name file)
  1659.            :file-position
  1660.            (multiple-value-bind (ignore pos)
  1661.                     (find-source-root tlf *source-info*)
  1662.              (declare (ignore ignore))
  1663.              pos)
  1664.            :original-source-path
  1665.            (source-path-original-source path))))))))))
  1666.  
  1667.  
  1668. ;;;; Printing error messages:
  1669.  
  1670. ;;; A function that is called to unwind out of Compiler-Error.
  1671. ;;;
  1672. (proclaim '(type (function () nil) *compiler-error-bailout*))
  1673. (defvar *compiler-error-bailout*
  1674.   #'(lambda () (error "Compiler-Error with no bailout.")))
  1675.  
  1676. ;;; The stream that compiler error output is directed to.
  1677. ;;;
  1678. (defvar *compiler-error-output* (make-synonym-stream '*error-output*))
  1679. (proclaim '(type stream *compiler-error-output*))
  1680.  
  1681. ;;; We save the context information that we printed out most recently so that
  1682. ;;; we don't print it out redundantly.
  1683.  
  1684. ;;; The last COMPILER-ERROR-CONTEXT that we printed.
  1685. ;;;
  1686. (defvar *last-error-context* nil)
  1687. (proclaim '(type (or compiler-error-context null) *last-error-context*))
  1688.  
  1689. ;;; The format string and args for the last error we printed.
  1690. ;;;
  1691. (defvar *last-format-string* nil)
  1692. (defvar *last-format-args* nil)
  1693. (proclaim '(type (or string null) *last-format-string*))
  1694. (proclaim '(type list *last-format-args*))
  1695.  
  1696. ;;; The number of times that the last error message has been emitted, so that
  1697. ;;; we can compress duplicate error messages.
  1698. (defvar *last-message-count* 0)
  1699. (proclaim '(type index *last-message-count*))
  1700.  
  1701. (defvar *compiler-notification-function* nil
  1702.   "This is the function called by the compiler to specially note a warning,
  1703.    comment, or error.  The function must take four arguments, the severity
  1704.    a string for context, the file namestring, and the file position.  The
  1705.    severity is one of :note, :warning, or :error.  Except for the severity, all
  1706.    of these can be NIL if unavailable or inapplicable.")
  1707.  
  1708.  
  1709. ;;; COMPILER-NOTIFICATION  --  Internal
  1710. ;;;
  1711. ;;;    Call any defined notification function.
  1712. ;;;
  1713. (defun compiler-notification (severity context)
  1714.   (declare (type (member :note :warning :error) severity)
  1715.        (type (or compiler-error-context null) context))
  1716.   (when *compiler-notification-function*
  1717.     (if context
  1718.     (let ((*print-level* 2)
  1719.           (*print-pretty* nil)
  1720.           (name (compiler-error-context-file-name context)))
  1721.       (funcall *compiler-notification-function* severity 
  1722.            (format nil "~{~{~S~^ ~}~^ => ~}"
  1723.                (compiler-error-context-context context))
  1724.            (when (stringp name) name)
  1725.            (compiler-error-context-file-position context)))
  1726.     (funcall *compiler-notification-function* severity nil nil nil)))
  1727.   (undefined-value))
  1728.  
  1729.  
  1730. ;;; Note-Message-Repeats  --  Internal
  1731. ;;;
  1732. ;;;    If the last message was given more than once, then print out an
  1733. ;;; indication of how many times it was repeated.  We reset the message count
  1734. ;;; when we are done.
  1735. ;;;
  1736. (defun note-message-repeats (&optional (terpri t))
  1737.   (cond ((= *last-message-count* 1)
  1738.      (when terpri (terpri *compiler-error-output*)))
  1739.     ((> *last-message-count* 1)
  1740.      (format *compiler-error-output* "[Last message occurs ~D times]~2%"
  1741.          *last-message-count*)))
  1742.   (setq *last-message-count* 0))
  1743.  
  1744.  
  1745. ;;; Print-Error-Message  --  Internal
  1746. ;;;
  1747. ;;;    Print out the message, with appropriate context if we can find it.  If
  1748. ;;; If the context is different from the context of the last message we
  1749. ;;; printed, then we print the context.  If the original source is different
  1750. ;;; from the source we are working on, then we print the current source in
  1751. ;;; addition to the original source.
  1752. ;;;
  1753. ;;;    We suppress printing of messages identical to the previous, but record
  1754. ;;; the number of times that the message is repeated.
  1755. ;;;
  1756. (defun print-error-message (what format-string format-args)
  1757.   (declare (type (member :error :warning :note) what) (string format-string)
  1758.        (list format-args))
  1759.   (let* ((*print-level* (or *error-print-level* *print-level*))
  1760.      (*print-length* (or *error-print-length* *print-length*))
  1761.      (*print-lines* (or *error-print-lines* *print-lines*))
  1762.      (stream *compiler-error-output*)
  1763.      (context (find-error-context format-args)))
  1764.     (cond
  1765.      (context
  1766.       (let ((file (compiler-error-context-file-name context))
  1767.         (in (compiler-error-context-context context))
  1768.         (form (compiler-error-context-original-source context))
  1769.         (enclosing (compiler-error-context-enclosing-source context))
  1770.         (source (compiler-error-context-source context))
  1771.         (last *last-error-context*))
  1772.     (compiler-notification what context)
  1773.  
  1774.     (unless (and last
  1775.              (equal file (compiler-error-context-file-name last)))
  1776.       (when (stringp file)
  1777.         (note-message-repeats)
  1778.         (setq last nil)
  1779.         (format stream "~2&File: ~A~%" file)))
  1780.     
  1781.     (unless (and last
  1782.              (equal in (compiler-error-context-context last)))
  1783.       (note-message-repeats)
  1784.       (setq last nil)
  1785.       (format stream "~2&In:~{~<~%   ~4:;~{ ~S~}~>~^ =>~}~%" in))
  1786.     
  1787.     (unless (and last
  1788.              (string= form
  1789.                   (compiler-error-context-original-source last)))
  1790.       (note-message-repeats)
  1791.       (setq last nil)
  1792.       (write-string form stream))
  1793.     
  1794.     (unless (and last
  1795.              (equal enclosing
  1796.                 (compiler-error-context-enclosing-source last)))
  1797.       (when enclosing
  1798.         (note-message-repeats)
  1799.         (setq last nil)
  1800.         (format stream "--> ~{~<~%--> ~1:;~A~> ~}~%" enclosing)))
  1801.     
  1802.     (unless (and last
  1803.              (equal source (compiler-error-context-source last)))
  1804.       (setq *last-format-string* nil)
  1805.       (when source
  1806.         (note-message-repeats)
  1807.         (dolist (src source)
  1808.           (write-line "==>" stream)
  1809.           (write-string src stream))))))
  1810.      (t
  1811.       (compiler-notification what nil)
  1812.       (note-message-repeats)
  1813.       (setq *last-format-string* nil)
  1814.       (format stream "~2&")))
  1815.  
  1816.     (setq *last-error-context* context)
  1817.     
  1818.     (unless (and (equal format-string *last-format-string*)
  1819.          (tree-equal format-args *last-format-args*))
  1820.       (note-message-repeats nil)
  1821.       (setq *last-format-string* format-string)
  1822.       (setq *last-format-args* format-args)
  1823.       (format stream "~&~:(~A~): ~?~&" what format-string format-args)))
  1824.   
  1825.   (incf *last-message-count*)
  1826.   (undefined-value))
  1827.  
  1828.  
  1829. ;;; Keep track of how many times each kind of warning happens.
  1830. ;;;
  1831. (proclaim '(type index *compiler-error-count* *compiler-warning-count*
  1832.          *compiler-note-count*))
  1833. (defvar *compiler-error-count* 0)
  1834. (defvar *compiler-warning-count* 0)
  1835. (defvar *compiler-note-count* 0)
  1836.  
  1837.  
  1838. ;;; Compiler-Error, ...  --  Interface
  1839. ;;;
  1840. ;;;    Increment the count and print the message.  Compiler-Note never prints
  1841. ;;; anything when Brevity is 3.  Compiler-Error calls the bailout function
  1842. ;;; so that it never returns.  Compiler-Error-Message returns like
  1843. ;;; Compiler-Warning, but prints a message like Compiler-Error.
  1844. ;;;
  1845. (proclaim '(ftype (function (string &rest t) void)
  1846.           compiler-error compiler-warning compiler-note))
  1847. ;;;
  1848. (defun compiler-error (format-string &rest format-args)
  1849.   (incf *compiler-error-count*)
  1850.   (print-error-message :error format-string format-args)
  1851.   (funcall *compiler-error-bailout*)
  1852.   (error "*Compiler-Error-Bailout* returned?"))
  1853. ;;;
  1854. (defun compiler-error-message (format-string &rest format-args)
  1855.   (incf *compiler-error-count*)
  1856.   (print-error-message :error format-string format-args))
  1857. ;;;
  1858. (defun compiler-warning (format-string &rest format-args)
  1859.   (incf *compiler-warning-count*)
  1860.   (print-error-message :warning format-string format-args))
  1861. ;;;
  1862. (defun compiler-note (format-string &rest format-args)
  1863.   (unless (if *compiler-error-context*
  1864.           (policy *compiler-error-context* (= brevity 3))
  1865.           (policy nil (= brevity 3)))
  1866.     (incf *compiler-note-count*)
  1867.     (print-error-message :note format-string format-args)))
  1868.  
  1869.  
  1870. ;;; Compiler-Mumble  --  Interface
  1871. ;;;
  1872. ;;;    The politically correct way to print out random progress messages and
  1873. ;;; such like.  We clear the current error context so that we know that it
  1874. ;;; needs to be reprinted, and we also Force-Output so that the message gets
  1875. ;;; seen right away.
  1876. ;;;
  1877. (proclaim '(function compiler-mumble (string &rest t) void))
  1878. (defun compiler-mumble (format-string &rest format-args)
  1879.   (note-message-repeats)
  1880.   (setq *last-error-context* nil)
  1881.   (apply #'format *compiler-error-output* format-string format-args)
  1882.   (force-output *compiler-error-output*))
  1883.  
  1884.  
  1885. ;;; Find-Component-Name  --  Interface
  1886. ;;;
  1887. ;;;    Return a string that somehow names the code in Component.  We use the
  1888. ;;; source path for the bind node for an arbitrary entry point to find the
  1889. ;;; source context, then return that as a string.
  1890. ;;;
  1891. (proclaim  '(function find-component-name (component) simple-string))
  1892. (defun find-component-name (component)
  1893.   (let ((ep (first (block-succ (component-head component)))))
  1894.     (assert ep () "No entry points?")
  1895.     (multiple-value-bind
  1896.     (form context)
  1897.     (find-original-source
  1898.      (node-source-path (continuation-next (block-start ep))))
  1899.       (declare (ignore form))
  1900.       (let ((*print-level* 2)
  1901.         (*print-pretty* nil))
  1902.     (format nil "~{~{~S~^ ~}~^ => ~}" context)))))
  1903.  
  1904.  
  1905. ;;;; Undefined warnings:
  1906.  
  1907.  
  1908. (defvar *undefined-warning-limit* 3
  1909.   "If non-null, then an upper limit on the number of unknown function or type
  1910.   warnings that the compiler will print for any given name in a single
  1911.   compilation.  This prevents excessive amounts of output when there really is
  1912.   a missing definition (as opposed to a typo in the use.)")
  1913.  
  1914.  
  1915. ;;; NOTE-UNDEFINED-REFERENCE  --  Interface
  1916. ;;;
  1917. ;;;    Make an entry in the *UNDEFINED-WARNINGS* describing a reference to Name
  1918. ;;; of the specified Kind.  If we have exceeded the warning limit, then just
  1919. ;;; increment the count, otherwise note the current error context.
  1920. ;;;
  1921. (defun note-undefined-reference (name kind)
  1922.   (unless (policy nil (= brevity 3))
  1923.     (let* ((found (dolist (warn *undefined-warnings* nil)
  1924.             (when (and (equal (undefined-warning-name warn) name)
  1925.                    (eq (undefined-warning-kind warn) kind))
  1926.               (return warn))))
  1927.        (res (or found
  1928.             (make-undefined-warning :name name :kind kind))))
  1929.       (unless found (push res *undefined-warnings*))
  1930.       (when (or (not *undefined-warning-limit*)
  1931.         (< (undefined-warning-count res) *undefined-warning-limit*))
  1932.     (push (find-error-context (list name))
  1933.           (undefined-warning-warnings res)))
  1934.       (incf (undefined-warning-count res))))
  1935.   (undefined-value))
  1936.  
  1937.  
  1938. ;;;; Careful call:
  1939.  
  1940. ;;; Careful-Call  --  Interface
  1941. ;;;
  1942. ;;;    Apply a function to some arguments, returning a list of the values
  1943. ;;; resulting of the evaulation.  If an error is signalled during the
  1944. ;;; application, then we print a warning message and return NIL as our second
  1945. ;;; value to indicate this.  Node is used as the error context for any error
  1946. ;;; message, and Context is a string that is spliced into the warning.
  1947. ;;;
  1948. (proclaim '(function careful-call ((or symbol function) list node string)
  1949.              (values list boolean)))
  1950. (defun careful-call (function args node context)
  1951.   (values
  1952.    (multiple-value-list
  1953.     (handler-case (apply function args)
  1954.       (error (condition)
  1955.     (let ((*compiler-error-context* node))
  1956.       (compiler-warning "Lisp error during ~A:~%~A" context condition)
  1957.       (return-from careful-call (values nil nil))))))
  1958.    t))
  1959.  
  1960.  
  1961. ;;;; Generic list (?) functions:
  1962.  
  1963. (proclaim '(inline find-in position-in map-in))
  1964.  
  1965. ;;; Find-In  --  Interface
  1966. ;;;
  1967. (defun find-in (next element list &key (key #'identity)
  1968.              (test #'eql test-p) (test-not nil not-p))
  1969.   "Find Element in a null-terminated List linked by the accessor function
  1970.   Next.  Key, Test and Test-Not are the same as for generic sequence
  1971.   functions."
  1972.   (when (and test-p not-p)
  1973.     (error "Silly to supply both :Test and :Test-Not."))
  1974.   (if not-p
  1975.       (do ((current list (funcall next current)))
  1976.       ((null current) nil)
  1977.     (unless (funcall test-not (funcall key current) element)
  1978.       (return current)))
  1979.       (do ((current list (funcall next current)))
  1980.       ((null current) nil)
  1981.     (when (funcall test (funcall key current) element)
  1982.       (return current)))))
  1983.  
  1984. ;;; Position-In  --  Interface
  1985. ;;;
  1986. (defun position-in (next element list &key (key #'identity)
  1987.              (test #'eql test-p) (test-not nil not-p))
  1988.   "Return the position of Element (or NIL if absent) in a null-terminated List
  1989.   linked by the accessor function Next.  Key, Test and Test-Not are the same as
  1990.   for generic sequence functions."
  1991.   (when (and test-p not-p)
  1992.     (error "Silly to supply both :Test and :Test-Not."))
  1993.   (if not-p
  1994.       (do ((current list (funcall next current))
  1995.        (i 0 (1+ i)))
  1996.       ((null current) nil)
  1997.     (unless (funcall test-not (funcall key current) element)
  1998.       (return i)))
  1999.       (do ((current list (funcall next current))
  2000.        (i 0 (1+ i)))
  2001.       ((null current) nil)
  2002.     (when (funcall test (funcall key current) element)
  2003.       (return i)))))
  2004.  
  2005.  
  2006. ;;; Map-In  --  Interface
  2007. ;;;
  2008. (defun map-in (next function list)
  2009.   "Map Function over the elements in a null-terminated List linked by the
  2010.   accessor function Next, returning a list of the results."
  2011.   (collect ((res))
  2012.     (do ((current list (funcall next current)))
  2013.     ((null current))
  2014.       (res (funcall function current)))
  2015.     (res)))
  2016.  
  2017.  
  2018. ;;; Deletef-In  --  Interface
  2019. ;;;
  2020. (defmacro deletef-in (next place item &environment env)
  2021.   "Deletef-In Next Place Item
  2022.   Delete Item from a null-terminated list linked by the accessor function Next
  2023.   that is stored in Place.  Item must appear exactly once in the list."
  2024.   (multiple-value-bind
  2025.       (temps vals stores store access)
  2026.       (get-setf-method place env)
  2027.     (let ((n-item (gensym))
  2028.       (n-place (gensym))
  2029.       (n-current (gensym))
  2030.       (n-prev (gensym)))
  2031.       `(let* (,@(mapcar #'list temps vals)
  2032.           (,n-place ,access)
  2033.           (,n-item ,item))
  2034.      (if (eq ,n-place ,n-item)
  2035.          (let ((,(first stores) (,next ,n-place)))
  2036.            ,store)
  2037.          (do ((,n-prev ,n-place ,n-current)
  2038.           (,n-current (,next ,n-place)
  2039.                   (,next ,n-current)))
  2040.          ((eq ,n-current ,n-item)
  2041.           (setf (,next ,n-prev)
  2042.             (,next ,n-current)))))
  2043.      (undefined-value)))))
  2044.  
  2045.  
  2046. ;;; Push-In  --  Interface
  2047. ;;;
  2048. (defmacro push-in (next item place &environment env)
  2049.   "Push Item onto a list linked by the accessor function Next that is stored in
  2050.   Place."
  2051.   (multiple-value-bind
  2052.       (temps vals stores store access)
  2053.       (get-setf-method place env)
  2054.     `(let (,@(mapcar #'list temps vals)
  2055.        (,(first stores) ,item))
  2056.        (setf (,next ,(first stores)) ,access)
  2057.        ,store
  2058.        (undefined-value))))
  2059.  
  2060.  
  2061. ;;; Compiler-Constantp  --  Interface
  2062. ;;;
  2063. ;;;    We don't want to assume that a variable is a constant just because it is
  2064. ;;; in the current lisp environment.
  2065. ;;;
  2066. ;;; ### For now, just use CONSTANTP to avoid bootstrapping problems with having
  2067. ;;; to have the INFO database available at meta-compile time.
  2068. ;;;
  2069. (proclaim '(function compiler-constantp (t) boolean))
  2070. (defun compiler-constantp (exp)
  2071.   "Like constantp, only uses the compilation environment rather than the
  2072.   current Lisp environment."
  2073. #|
  2074.   (if (symbolp exp)
  2075.       (eq (info variable kind exp) :constant)
  2076.       (constantp exp))
  2077. |#
  2078.   (constantp exp))
  2079.